﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Global.Class.Library;
using DataTransferObject;
using DataAccessLayer;
using BusinessLogicLayer;
using DataAccessLayer.GCGBWHOutputDTSTableAdapters;

namespace PresentationLayer
{
    public partial class WizardGCGBWHOutputNew : Form
    {
        private GCGBWHOutputMaster gcgbWHOutputMaster;
        private int gcgbCommandID;

        private CurrencyManager gcgbCommandCurrencyManager;

        private DataTable gcgbCommandDataTable;

        public int GCGBCommandID
        {
            get { return this.gcgbCommandID; }
            set
            {
                if (this.gcgbCommandID != value)
                {
                    this.gcgbCommandID = value;
                }
            }
        }

        GCGBWHOutputWizardGetTableAdapter getWizardTableAdapter;
        protected GCGBWHOutputWizardGetTableAdapter GetWizardTableAdapter
        {
            get
            {
                if (getWizardTableAdapter == null) getWizardTableAdapter = new GCGBWHOutputWizardGetTableAdapter();
                return getWizardTableAdapter;
            }
        }

        
        public WizardGCGBWHOutputNew(GCGBWHOutputMaster gcgbWHOutputMaster)
        {
            InitializeComponent();

            try
            {
                if (GlobalVariables.shouldRestoreProcedure) RestoreProcedure();

                this.gcgbWHOutputMaster = gcgbWHOutputMaster;                 

                GCGBWHOutputDTS.GCGBWHOutputWizardGetDataTable gcgbCommandDataTable = this.GetWizardTableAdapter.GetData(this.gcgbWHOutputMaster.GCGBWHOutputID);

                if (gcgbCommandDataTable.Rows.Count > 0)
                {
                    gcgbCommandDataTable.PrimaryKey = new DataColumn[] { gcgbCommandDataTable.Columns["GCGBCommandID"] };

                    this.GCGBCommandID = this.gcgbWHOutputMaster.GCGBCommandID;

                    this.dataGridViewBG1.AutoGenerateColumns = false;
                    this.dataGridViewBG1.DataSource = gcgbCommandDataTable;

                    gcgbCommandCurrencyManager = (CurrencyManager)this.BindingContext[gcgbCommandDataTable];

                    this.DataBindings.Add("GCGBCommandID", gcgbCommandDataTable, "GCGBCommandID");


                    gcgbCommandCurrencyManager.Position = gcgbCommandDataTable.Rows.IndexOf(gcgbCommandDataTable.Rows.Find(this.GCGBCommandID));
                }

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        } 

        private void RestoreProcedure()
        {
            string queryString;

            queryString = "     @GCGBWHOutputID int " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + " SELECT DISTINCT GCGBCommandCollection.GCGBCommandID, GCGBCommandCollection.GCGBCommandReference, GCGBCommandCollection.GCGBCommandDate, GCGBCommandMaster.GCGBCommandTypeID, GCGBCommandMaster.EntryDate, GCGBCommandMaster.Description, GCGBCommandMaster.Remarks, GCGBCommandCollection.GCGBCommandReference + ' [' + CONVERT(char(10), GCGBCommandCollection.GCGBCommandDate, 103) + ']' AS GCGBCommandReferenceAndDate ";
            queryString = queryString + " FROM GCGBCommandCollection INNER JOIN GCGBCommandMaster ON GCGBCommandCollection.GCGBCommandID = GCGBCommandMaster.GCGBCommandID ";
            queryString = queryString + "WHERE GCGBCommandCollection.GCGBCommandID IN (SELECT GCGBCommandID FROM GCGBWHOutputMaster WHERE GCGBWHOutputID = @GCGBWHOutputID) OR ROUND(GCGBCommandCollection.Quantity - GCGBCommandCollection.QuantityWHOutput - GCGBCommandCollection.QuantityWHOutputLoss, 0) > 0 ";

            SQLDatabase.CreateStoredProcedure("GCGBWHOutputWizardGet", queryString);
        }
       
        
        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                this.gcgbWHOutputMaster.GCGBCommandID = this.GCGBCommandID;                
                this.gcgbWHOutputMaster.GCGBCommandReference = this.dataGridViewBG1.CurrentRow.Cells["GCGBCommandReference"].Value.ToString();
                this.gcgbWHOutputMaster.GCGBCommandDate = Convert.ToDateTime(this.dataGridViewBG1.CurrentRow.Cells["GCGBCommandDate"].Value);
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

    }
}
